package com.fingard.dsp.bank.directbank.zmh01.util;

import org.apache.commons.lang3.RandomUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
import java.util.Arrays;
import java.util.Date;

public class Sm4Util {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private static final String ENCODING = "UTF-8";

    public static final String ALGORITHM_NAME = "SM4";

    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";

    public static final int DEFAULT_KEY_SIZE = 128;

    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
        Key sm4key = new SecretKeySpec(key, ALGORITHM_NAME);
        cipher.init(mode, sm4key);
        return cipher;
    }

    public static String encryptEcb(String hexKey, String paramStr) throws Exception {
        String cipherText = "";
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        byte[] srcData = paramStr.getBytes(ENCODING);
        byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
        cipherText = ByteUtils.toHexString(cipherArray);
        return cipherText;

    }

    public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }


    //    解密
    public static String decryptEcb(String hexKey, String cipherText) throws Exception {
        String decryptStr = "";
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        byte[] cipherData = ByteUtils.fromHexString(cipherText);
        byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
        decryptStr = new String(srcData, ENCODING);
        return decryptStr;
    }

    public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(cipherText);
    }

    //    加密数据校验
    public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
        boolean flag = false;

        byte[] keyData = ByteUtils.fromHexString(hexKey);

        byte[] cipherData = ByteUtils.fromHexString(cipherText);

        byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);

        byte[] srcData = paramStr.getBytes(ENCODING);

        flag = Arrays.equals(decryptData, srcData);

        return flag;
    }

    /*public static void main(String[] args) {
        try {
            String json = "{\"OrderId\":\"206501500430661910221015\",\"token\":\"zmjk-eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJzdWIiOiLkvJfnvo7lgaXlurciLCJmdWxsbmFtZSI6InRhaWthbmciLCJjbGllbnRpZCI6IjEwNDQ1MDYwNzMxOTAzNjc2NDAiLCJ1c2VybmFtZSI6InRhaWthbmciLCJwYXNzd29yZCI6IjA2ZTIwNjAxYTdlYWVlNWIiLCJhdWQiOiJ0YWlrYW5nIiwiaXNzIjoiem1qay1jdCIsImlhdCI6MTU3MzQzNTY5OCwiZXhwIjoxNTczNTIyMDk4LCJuYmYiOjE1NzM0MzU2OTh9.LHl75l2B4xbdFPIoJObWvNmt2mGITtbQF3WeKY_8nGk\"}";
            // 自定义的32位16进制密钥
            String key = "86C63180C2806ED1F47B859DE501215B";
            String cipher = Sm4Util.encryptEcb(key, json);
            System.out.println(cipher);//05a087dc798bb0b3e80553e6a2e73c4ccc7651035ea056e43bea9d125806bf41c45b4263109c8770c48c5da3c6f32df444f88698c5c9fdb5b0055b8d042e3ac9d4e3f7cc67525139b64952a3508a7619
            System.out.println(Sm4Util.verifyEcb(key, cipher, json));// true
            json = Sm4Util.decryptEcb(key, cipher);
            System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}